<template>
  <div class="mod-card__config">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="200px">
      <el-form-item :label="$t('dealer.dealerId')" prop="dealerId">
        <el-select v-model="dataForm.dealerId" clearable>
          <el-option v-for="item in dealerList" :key="item.id" :label="item.dealerName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('dealer.webServiceIp')" prop="webServiceIp">
        <el-input v-model="dataForm.webServiceIp"></el-input>
      </el-form-item>
      <el-form-item :label="$t('dealer.webServicePort')" prop="webServicePort">
        <el-input-number v-model="dataForm.webServicePort" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("save") }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();

const dataForm = reactive({
  id: "",
  dealerId: null,
  webServiceIp: '',
  webServicePort: null
});
const dataFormRef = ref();
const dealerList = ref<any[]>([]);

const rules = ref({
  dealerId: [{ required: true, message: t("validate.required"), trigger: "change" }],
  webServiceIp: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  webServicePort: [{ required: true, message: t("validate.required"), trigger: "change" }],
});

// 获取商户列表
const getDealerList = () => {
  return baseService.get("/card/zy/dealer/list").then((res) => {
    dealerList.value = res.data;
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    baseService.post(`/card/zy/config/${!dataForm.id ? 'save' : 'update'}`, dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
        }
      });
    });
  });
};

onMounted(() => {
  getDealerList();
});
</script>
